package com.cjf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjf.comon.CustomException;
import com.cjf.comon.DishMapper;
import com.cjf.dto.DishDto;
import com.cjf.entity.DishFlavor;
import com.cjf.entity.Dish;
import com.cjf.entity.Setmeal;
import com.cjf.entity.SetmealDish;
import com.cjf.service.DishFlavorService;
import com.cjf.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private DishService dishService;

    /***
     * 新增菜品  同时需保存对应口味数据
     * @param dishDto
     */
    public void saveWithFlavor(@RequestBody DishDto dishDto) {
        //保存菜品的基本信息到菜品表dish
        this.save(dishDto);

        //获取菜品id
        Long dishId = dishDto.getId();
        List<DishFlavor> flavors = dishDto.getFlavors();

        //遍历菜品口味
        flavors.stream().map((item) -> {
            item.setDishId(dishId);
           return item;
        }).collect(Collectors.toList());

        //保存菜品口味数据到菜品口味表dish_flavor
        dishFlavorService.saveBatch(flavors);
    }

    /***
     * 根据id查询菜品信息和口味信息
     * @param id
     * @return
     */
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品基本信息 从dish表查
        Dish dish = this.getById(id);

        DishDto dishDto = new DishDto();

        BeanUtils.copyProperties(dish,dishDto);

        //查询当前菜品对应的口味信息 从dish_flavor表查询
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(flavors);

        return dishDto;
    }

    @Override
    @Transactional
    public void updateWithFlavor(@RequestBody DishDto dishDto) {

        //更新dish表的数据
        this.updateById(dishDto);

        //清理当前菜品的口味数据   dish_flavor表  delete操作
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();//创建条件构造器
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());//添加过滤条件
        dishFlavorService.remove(queryWrapper);//执行删除操作

        //添加当前提交的菜品口味数据  dish_flavor  insert操作
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors =  flavors.stream().map(new Function<DishFlavor, DishFlavor>() {
            @Override
            public DishFlavor apply(DishFlavor item) {
                item.setDishId(dishDto.getId());
                return item;
            }
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public void removeWithDish(List<Long> ids) {
        //查询菜品状态 确定是否可以删除 起售状态下不可删除
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Dish::getId,ids);
        queryWrapper.eq(Dish::getStatus,1);
        int count = this.count(queryWrapper);
        if (count>0) {
            throw new CustomException("该菜品处于售卖中，不能删除!");
        }
        //删除dish表的数据
        this.removeByIds(ids);

        LambdaQueryWrapper<DishFlavor> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.in(DishFlavor::getDishId,ids);
        //删除dish_flavor表中的数据
        dishFlavorService.remove(queryWrapper1);
    }


}
